package cn.petplatform.petback.service.adoptpets.impl;

import cn.petplatform.dto.ResponseDto;
import cn.petplatform.petback.exceptions.DataTransformException;
import cn.petplatform.petback.mapper.AdoptPetsDetailMapper;
import cn.petplatform.petback.mapper.PetAdoptMapper;
import cn.petplatform.petback.mapper.PetMapper;
import cn.petplatform.petback.pojo.Pet;
import cn.petplatform.petback.pojo.PetAdopt;
import cn.petplatform.petback.pojo.vo.AdoptInfoVo;
import cn.petplatform.petback.pojo.vo.PublishPetDetailVo;
import cn.petplatform.petback.service.adoptpets.AdoptPetsService;
import cn.petplatform.utils.JWTUtils;
import cn.petplatform.utils.UUIDUtils;
import com.auth0.jwt.interfaces.DecodedJWT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author LNL
 * @date 2021/5/17 16:35
 */
@Service("adoptPetsService")
@Transactional
public class AdoptPetsServiceImpl implements AdoptPetsService {

    @Autowired
    private AdoptPetsDetailMapper adoptPetsDetailMapper;

    @Autowired
    private PetAdoptMapper petAdoptMapper;

    @Autowired
    private PetMapper petMapper;

    @Override
    public ResponseDto getAdoptPets() {
        ResponseDto responseDto = null;

        List<AdoptInfoVo> adoptPets = adoptPetsDetailMapper.getAdoptPets();

        Optional<List<AdoptInfoVo>> optionalList = Optional.ofNullable(adoptPets);
        if (optionalList.isPresent()) {//如果不为空
            responseDto = new ResponseDto("1000", "领养宠物信息", adoptPets);
        } else {
            throw new DataTransformException("数据异常，请联系管理员");
        }
        return responseDto;
    }


    @Override
    public ResponseDto getAdoptPetsByPerson(String id) {
        ResponseDto responseDto = null;
        if (id == null || "".equals(id)) {
            throw new RuntimeException("id不能为空");
        }
        List<AdoptInfoVo> adoptPets = adoptPetsDetailMapper.getAdoptPetsByPerson(id);
        Optional<List<AdoptInfoVo>> optionalList = Optional.ofNullable(adoptPets);
        if (optionalList.isPresent()) {//如果不为空
            responseDto = new ResponseDto("1000", "个人领养宠物", adoptPets);
        } else {
            throw new DataTransformException("数据异常，请联系管理员");
        }
        return responseDto;
    }

    @Override
    public ResponseDto deleteAdoptPetById(String id) {
        ResponseDto responseDto = null;
        //获取当前宠物的id
        PetAdopt petAdopt = petAdoptMapper.selectByPrimaryKey(id);

        //删除领养数据
        int isDelete = petAdoptMapper.deleteByPrimaryKey(id);
        if (isDelete > 0) {
            //删除领养时，将宠物的领养情况设置为未领养
            Pet pet = new Pet();
            pet.setId(petAdopt.getPetId());
            pet.setIsAdopt(0);
            //去修改宠物领养状态
            int isUpdate = petMapper.updateByPrimaryKeySelective(pet);
            if (isUpdate > 0) {
                responseDto = new ResponseDto("1000", "删除成功");
            } else {
                throw new RuntimeException("删除失败");
            }
        } else {
            responseDto = new ResponseDto("1001", "删除失败");
        }
        return responseDto;
    }

    @Override
    public ResponseDto insertAdoptPetByPerson(String petId, HttpServletRequest request) {
        ResponseDto responseDto = null;
        //获取当前用户信息
        DecodedJWT userInfo = JWTUtils.getUserInfo(request.getHeader("token"));

        //定义一个宠物领养类
        PetAdopt petAdopt = new PetAdopt();
        petAdopt.setId(UUIDUtils.getUUID());
        petAdopt.setAdoptTime(new Date());
        petAdopt.setPetId(petId);
        petAdopt.setUserId(userInfo.getClaim("userId").asString());
        petAdopt.setCreateTime(new Date());
        petAdopt.setCreateUser(userInfo.getClaim("userId").asString());

        int isInsert = petAdoptMapper.insertSelective(petAdopt);
        if (isInsert > 0) {
            responseDto = new ResponseDto("1000", "新增成功");
        } else {
            responseDto = new ResponseDto("1001", "新增失败");
        }
        return responseDto;
    }

    @Override
    public ResponseDto getAdoptPetsByAdoptId(String id) {
        ResponseDto responseDto = null;
        AdoptInfoVo adoptInfoVo = adoptPetsDetailMapper.getAdoptPetsByAdoptId(id);
        Optional<AdoptInfoVo> optionalAdoptInfoVo = Optional.ofNullable(adoptInfoVo);
        if (optionalAdoptInfoVo.isPresent()) {
            responseDto = new ResponseDto("1000", "获取数据成功", adoptInfoVo);
        } else {
            responseDto = new ResponseDto("1001", "获取数据失败");
        }
        return responseDto;
    }

    @Override
    public ResponseDto checkAdopt(String id, HttpServletRequest request) {
        //审核状态
        ResponseDto responseDto = null;
        try {
            //模拟一个正在审核的状态
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //获取修改人数据
        DecodedJWT userInfo = JWTUtils.getUserInfo(request.getHeader("token"));
        PetAdopt petAdopt = new PetAdopt();
        petAdopt.setId(id);
        petAdopt.setIsPass(1);
        petAdopt.setEditUser(userInfo.getClaim("userId").asString());
        petAdopt.setEditTime(new Date());

        int isCheck = petAdoptMapper.updateByPrimaryKeySelective(petAdopt);
        //获取宠物信息并修改宠物的is_adopt为1
        PetAdopt adopt = petAdoptMapper.selectByPrimaryKey(id);
        //  审核
        int isUpdate = petMapper.updateIsAdoptAsOne(adopt.getPetId());


        if (isCheck > 0 && isUpdate > 0) {
            responseDto = new ResponseDto("1000", "审核成功");
        } else {
            throw new RuntimeException("审核失败");
        }
        return responseDto;
    }

    @Override
    public ResponseDto addCareCount(Pet pet) {
        ResponseDto responseDto = null;
        //通过主键修改数据，isUpdate：返回的 是一个数字，成功返回1，失败返回0
        int isUpdate = petMapper.updateByPrimaryKeySelective(pet);

        if (isUpdate > 0) {
            responseDto = new ResponseDto("1000", "操作成功");
        } else {
            throw new RuntimeException("操作失败");
        }
        return responseDto;
    }

    @Override
    public ResponseDto getAdoptManagePets() {
        ResponseDto responseDto = null;

        List<AdoptInfoVo> adoptPets = adoptPetsDetailMapper.getAdoptManagePets();

        Optional<List<AdoptInfoVo>> optionalList = Optional.ofNullable(adoptPets);
        if (optionalList.isPresent()) {//如果不为空
            responseDto = new ResponseDto("1000", "领养宠物信息", adoptPets);
        } else {
            throw new DataTransformException("数据异常，请联系管理员");
        }
        return responseDto;
    }
}
